package com.xingshentech.ugv.operate.module.httpdata;

import com.xingshentech.ugv.operate.module.bean2.BaseResponseCenter;

import io.reactivex.functions.Function;

public class JsonResponseFunc<T> implements Function<BaseResponseCenter<T>, T> {

    public JsonResponseFunc() {
    }


    @Override
    public T apply(final BaseResponseCenter<T> baseResponse) throws Exception {
        if (baseResponse == null) return null;

        if (!baseResponse.isResponseCode()) {
            throw new ApiException(baseResponse);
        }
        if (!baseResponse.isStatus()) {
            throw new ApiException(baseResponse);
        }
        if (baseResponse.getData() != null) {
            //后台返回的base不太一样。希望不会同时出现 data 和result,如出现，单独在拆分一个吧
            return baseResponse.getData();
        }
        return baseResponse.getResult();
    }
}